package org.bulldog.playlist.program;

import org.bulldog.playlist.PlayListBuilder;
import org.bulldog.playlist.util.DateUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 插播日常节目
 *
 * @author zhanghongbin
 * @version 1.0
 */
class SpotsDailyPlayListBuilder implements PlayListBuilder<List<Program>[], Program> {

    public static final SpotsDailyPlayListBuilder INSTANCE = new SpotsDailyPlayListBuilder();

    private SpotsDailyPlayListBuilder() {

    }

    @Override
    public List<Program> build(List<Program>[] sportsDailyProgramArray) {
        List<Program> result = new ArrayList<>();
        if (sportsDailyProgramArray.length == 0) return result;
        String currentTime = DateUtil.now();
        String currentDate = currentTime.split(" ")[0] + " ";
        for (List<Program> programList : sportsDailyProgramArray) {
            if (programList.isEmpty()) continue;
            Program program = programList.get(0);
            String startTime = currentDate + program.getStartTime();
            String endTime = currentDate + program.getEndTime();
            List<Program> tempProgramList = this.build(programList, currentTime, startTime, endTime);
            if (!tempProgramList.isEmpty()) {
                //currentTime = tempProgramList.get(tempProgramList.size() - 1).getEndTime();
                result.addAll(tempProgramList);
            }
            programList.clear();
        }
        return result;
    }

    private List<Program> build(List<Program> programList, String currentTime, String startTime, String endTime) {
        List<Program> result = new ArrayList<>();
        int count = 0;
        label1:
        while (true) {
            for (int i = 0; i < programList.size(); i++) {
                if (count == 0) {
                    this.build(result, currentTime, startTime, endTime, programList.get(i));
                } else {
                    String programCurrentTime = result.get(count - 1).getEndTime();
                    this.build(result, programCurrentTime, programCurrentTime, endTime, programList.get(i));
                }
                count = count + 1;
                if (result.size() != count) {
                    if (!result.isEmpty()) {
                        //如果是最后一个，会设置结束时间，重新计算播放时长,有可能播放时长比实际时长长或是短
                        Program program1 = result.get(result.size() - 1);
                        program1.setEndTime(endTime);
                        program1.setPlayDuration(DateUtil.minus(program1.getStartTime(), endTime));
                    }
                    break label1;
                }
            }
        }
        return result;
    }

    private void build(List<Program> result, String currentTime, String startTime, String endTime, Program program) {
        int n = DateUtil.minus(currentTime, endTime);
        if (n < DateUtil.MIN_PLAY_DURATION) {
            return;
        }
        int startIndex = DateUtil.compare(currentTime, startTime);
        //设置当前节目开始时间
        String tempStartTime;
        if (startIndex == -1) {
            tempStartTime = startTime;
        } else {
            tempStartTime = currentTime;
        }
        n = DateUtil.minus(startTime, endTime);
        String tempEndTime;
        int playDuration;
        if (program.getDuration() < n) {
            playDuration = program.getDuration();
            tempEndTime = DateUtil.add(tempStartTime, program.getDuration());
        } else {
            playDuration = n;
            tempEndTime = endTime;
        }
        Program tempProgram = new Program(program.getId(), program.getName(), program.getUrl(), tempStartTime,
                tempEndTime, program.getDuration());
        tempProgram.setPlayDuration(playDuration);
        tempProgram.getParams().putAll(program.getParams());
        result.add(tempProgram);
    }
}
